<?php 

class CategoryController extends Controller{
	//将表定义为属性
	public $db;
	//构造函数格式化表
	public function __init(){
		$this->db=K('Category');
		$goodstype=M('goods_type')->all();
		$this->assign('goodstype',$goodstype);
		$category=$this->db->all();
		$this->assign('category',$category);
	}
	//index方法显示已存在的栏目列表
	public function index(){
		$data=$this->db->all();
		$data=Data::tree($data,'catname');
		$this->assign('data',$data);
		$this->display();
	}
	//add方法添加栏目
	public function add(){
		if (IS_POST) {
			// p($_POST);DIE;
			// p($this->db->addCategory());DIE;
			if ($this->db->addCategory()) {
				$this->success('添加栏目成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$goodstype=M('goods_type')->all();
			$this->assign('goodstype',$goodstype);
			$this->display();
		}
	}
	//edit方法编辑栏目
	public function edit(){
		if (IS_POST) {
			// p($_POST);die;
			if ($this->db->save($_POST)) {
				$this->success('修改栏目成功','index');
			}else{
				$this->error('失败');
			}
		}else{
			$cid=Q('cid','','intval');
			$cat=$this->db->where('cid='.$cid)->all();
			// p($cat);die;
			$this->assign('cat',$cat);
			$this->display();
		}
	}
	//addChild方法添加子栏目
	public function addChild(){
		if (IS_POST) {
			if ($this->db->addCategory()) {
				$this->success('添加子栏目成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$cid=Q('cid','','intval');
			$cat=$this->db->where('cid='.$cid)->all();
			// p($cat);die;
			$this->assign('cat',$cat);
			$this->display();
		}
	}
	//del删除栏目
	public function del(){
		// p($this->db->delCategory());die;
		if ($this->db->delCategory()) {
			$this->success('删除栏目成功','index');
		}else{
			$this->error('请先删除子栏目','index');
		}
	}


}


 ?>